package com.example.lycdays.ui.add;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lycdays.R;
import com.example.lycdays.data.bean.Event;
import com.example.lycdays.data.repositiory.EventDataBase;
import com.example.lycdays.ui.detail.DetailActivity;

import java.util.Calendar;

public class AddActivity extends AppCompatActivity {

    // 状态码，检测用户是否输入事件名、事件日期
    private static final int NO_EVENT_NAME = 1001;
    private static final int NO_EVENT_DATE = 1002;
    private static final int OTHER = 1000;


    int mYear;
    int mMonth;
    int mDay;
    String eventDate = null;

    EditText eventName, eventNote;
    Button buttonSubmit;
    View datePicker;
    TextView textViewDate;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        Toolbar mToolbar =findViewById(R.id.toolbarLogin);
        mToolbar.setTitle("添加");
        mToolbar.setTitleTextColor(getColor(R.color.colorWhite));
        setSupportActionBar(mToolbar);

        eventName = findViewById(R.id.editTextName);
        eventNote = findViewById(R.id.editTextNote);
        buttonSubmit = findViewById(R.id.buttonSubmit);
        textViewDate = findViewById(R.id.textViewDate);
        textViewDate.setVisibility(View.INVISIBLE);


        Calendar ca = Calendar.getInstance();
        mYear = ca.get(Calendar.YEAR);
        mMonth = ca.get(Calendar.MONTH);
        mDay = ca.get(Calendar.DAY_OF_MONTH);
        datePicker = findViewById(R.id.datePicker);
        datePicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DatePickerDialog(AddActivity.this, onDateSetListener, mYear, mMonth, mDay).show();
            }
        });

        buttonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isChoosed() == NO_EVENT_NAME) {
                    Toast.makeText(AddActivity.this, "请输入事件名", Toast.LENGTH_LONG).show();
                    return;
                }
                if (isChoosed() == NO_EVENT_DATE) {
                    Toast.makeText(AddActivity.this, "请选择日期", Toast.LENGTH_LONG).show();
                    return;
                }

                String name = eventName.getText().toString();
                String notes = eventNote.getText().toString();
                Event event = new Event(name, eventDate, notes);
                EventDataBase.insertEvent(event);
                Toast.makeText(AddActivity.this, "添加成功", Toast.LENGTH_LONG).show();

                Intent intent = new Intent(AddActivity.this, DetailActivity.class);
                //将event对象传入detail活动
                intent.putExtra("event", event);
                startActivity(intent);
            }
        });


    }


    /**
     * 检查用户是否输入事件名、事件时期
     * @return 状态码
     */
    private int isChoosed() {
        if ("".equals(eventName.getText().toString())) {
            return NO_EVENT_NAME;
        }
        if (eventDate == null) {
            return NO_EVENT_DATE;
        }
        return OTHER;
    }


    // 日期选择器的监听
    private DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            String days;
            if (mMonth + 1 < 10) {
                if (mDay < 10) {
                    days = mYear + "-" + "0" +
                            (mMonth + 1) + "-" + "0" + mDay;
                } else {
                    days = mYear + "-" + "0" +
                            (mMonth + 1) + "-" + mDay;
                }
            } else {
                if (mDay < 10) {
                    days = mYear + "-" +
                            (mMonth + 1) + "-" + "0" + mDay;
                } else {
                    days = mYear + "-" +
                            (mMonth + 1) + "-" + mDay;
                }
            }
            eventDate = days;
            textViewDate.setText(days);
            textViewDate.setVisibility(View.VISIBLE);
        }
    };

    @Override
    protected void onPause() {
        super.onPause();
        this.finish();
    }
}
